/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：CreatingDialog.java <br>
 * 创建时间：2013-5-21 下午4:07:07 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui.meta;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Arrays;

import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.R;
import lostland.gmud.exv2.blgframework.CScreen;
import lostland.gmud.exv2.data.MainChar;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.expand.BlankMenuScreen;
import lostland.gmud.exv2.ui.CreatingScreen;
import lostland.gmud.exv2.ui.CreatingScreen2;
import lostland.gmud.exv2.ui.StartScreen;

/**
 * 类名：CreatingDialog <br>
 * 说明：建立对话框
 *
 * @author 12548
 */
public class CreatingDialog extends Dialog {

	EditText etName;
	Button btnOK;
	RadioButton gender;


	public CreatingDialog() {
		super(Game.getInstance());
		Window mWindow = getWindow();
		mWindow.setBackgroundDrawable(new ColorDrawable(0));
		mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.gravity = Gravity.CENTER;
		lp.dimAmount = 0.0001f;
		mWindow.setAttributes(lp);
		setCanceledOnTouchOutside(false);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mWindow.setContentView(R.layout.dialog_create);


		etName = (EditText) findViewById(R.id.etName);
		gender = (RadioButton) findViewById(R.id.genderDefault);
		RadioGroup gpx = (RadioGroup) findViewById(R.id.genderGroup);
		gpx.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				gender = (RadioButton) findViewById(checkedId);
			}
		});

		btnOK = (Button) findViewById(R.id.btnOK);

		btnOK.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Gmud.mc = new MainChar();
					Gmud.mc.name = etName.getText().toString();


					Logger.w("1");

					ArrayList<String> zms = new ArrayList<>(6);
					zms.add(GameConstants.zm[0]);

					int maxzm;

					if (Game.getInstance().oldmc != null) {
						maxzm = Game.getInstance().oldmc.size();
					} else {
						maxzm = 0;
					}

					if(maxzm > 0) {
						zms.addAll(Arrays.asList(GameConstants.zm).subList(1, maxzm + 1));
					}

					if (maxzm > 0) {
						new BlankMenuScreen(zms, "请选择周目：") {

							@Override
							public void onClick(int i) {
								getGame().data.setSyuume(i);
								new StartMenuScreen().pushToGame();
							}

							@Override
							public void onCancel() {

							}
						}.replaceCurrent()
						;
					} else {
						new StartMenuScreen().pushToGame();
					}
					Logger.w("2");

					cancel();

				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			}
		});
	}

	private static final class StartMenuScreen extends BlankMenuScreen {

		public StartMenuScreen() {
			super(getMenuItems(), "请选择开局方式：");
		}

		private static ArrayList<String> getMenuItems() {
			ArrayList<String> t = new ArrayList<>();
			t.add("标准开局");
			t.add("以NPC属性开局");
			return t;
		}

		/* （非 Javadoc）
		 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#onCancel()
		 */
		@Override
		public void onCancel() {
			getGame().popScreen();
		}

		/* （非 Javadoc）
		 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#onClick(int)
		 */
		@Override
		public void onClick(int i) {
			if (i == 0) {
				new CreatingScreen().pushToGame();
			} else {
				ArrayList<String> npcName = new ArrayList<>();
				final ArrayList<Integer> npcID = new ArrayList<>();

				int syu = getGame().data.getSyuume() >= 5 ? 5 : getGame().data.getSyuume();
				Logger.d("The current syuume is %d.", syu);
				switch (syu) {
					case 5:
						npcID.add(Gmud.findNpcId("我是谁"));
						npcID.add(Gmud.findNpcId("东方求败"));
						npcID.add(126);
					case 4:
						npcID.add(Gmud.findNpcId("王维扬"));
						npcID.add(Gmud.findNpcId("李青照"));
						npcID.add(Gmud.findNpcId("余鸿儒"));
						npcID.add(Gmud.findNpcId("和仲阳"));
						npcID.add(Gmud.findNpcId("清虚道长"));
						npcID.add(Gmud.findNpcId("白瑞德"));
					case 3:
						npcID.add(Gmud.findNpcId("华长老"));
						npcID.add(Gmud.findNpcId("绿珠"));
					case 2:
						npcID.add(Gmud.findNpcId("护院武师"));
						npcID.add(Gmud.findNpcId("捕快"));
					case 1:
						npcID.add(49);
						npcID.add(29);
						npcID.add(12);
					case 0:
						npcID.add(1);
						npcID.add(157);
						npcID.add(11);
						npcID.add(9);
						npcID.add(106);
				}
				for (int j : npcID) {
					npcName.add(Gmud.getNpc(j).name);
				}
				CScreen scrn = new BlankMenuScreen(npcName, "请选择你要复制属性的npc") {

					@Override
					public void onCancel() {
						getGame().popScreen();
					}

					@Override
					public void onClick(int i) {
						int id = npcID.get(i);
						Npc npc = Gmud.getNpc(id);

						Gmud.mc.str = npc.str;
						Gmud.mc.agi = npc.agi;
						Gmud.mc.wxg = npc.wxg;
						Gmud.mc.bon = npc.bon;
						Gmud.mc.talent = npc.talent;

						new CreatingScreen2(npc.sex).pushToGame();
					}
				};

				scrn.pushToGame();


			}
		}
	}

	/* （非 Javadoc）
	 * @see android.app.Dialog#cancel()
	 */
	@Override
	public void cancel() {
		String name = etName.getText().toString().trim();
		if (Gmud.mc != null) {
			if (name.length() > 0) {
				Gmud.mc.name = name;
			} else {
				Gmud.mc.name = "无名氏";
			}

			if (Gmud.mc.name.length() > 8) {
				Gmud.mc.name = Gmud.mc.name.substring(0, 8);
			}

			if (gender.getText().toString()
					.contains(Game.tr(R.string.gender_m))) {
				Gmud.mc.sex = 0;
			} else if (gender.getText().toString()
					.contains(Game.tr(R.string.gender_f))) {
				Gmud.mc.sex = 1;
			} else if (gender.getText().toString()
					.contains(Game.tr(R.string.gender_r))) {
				Gmud.mc.sex = Gmud.rand.nextInt(2);
			} else {
				Gmud.mc.sex = -1;
			}
		}
		StartScreen.flag = true;

		super.cancel();
	}
}
